﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using CoreMVVM.ViewModels;
using System.Collections.ObjectModel;
using GeometryViz3D.Model;
using System.ComponentModel;

namespace GeometryViz3D.ViewModels
{
    public class LineViewModel : ElementViewModel
    {
        G3DLine m_line;

        PointViewModel m_startPoint, m_endPoint;

        public LineViewModel(IMainViewModel mainVM)
            : this(new G3DLine(), mainVM)
        {
        }

        public LineViewModel(G3DLine line, IMainViewModel mainVM)
            : base(line, mainVM)
        {
            MainViewModel.PropertyChanged += new PropertyChangedEventHandler(model_PropertyChanged);
            m_line = line;

            GetPoints();
        }

        void model_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Points")
            {
                OnPropertyChanged("AvailablePoints");

                PointViewModel sp = m_startPoint;
                PointViewModel ep = m_endPoint;

                StartPoint = null;
                EndPoint = null;
                StartPoint = sp;
                EndPoint = ep;
                //OnPropertyChanged("StartPoint");
                //OnPropertyChanged("EndPoint");
            }
        }

        #region Properties

        public ObservableCollection<PointViewModel> AvailablePoints
        {
            get { return MainViewModel.AllPoints; }
        }

        public PointViewModel StartPoint
        {
            get 
            {
                return m_startPoint;
            }
            set
            {
                m_startPoint = value;
                OnPropertyChanged("StartPoint");
                RaiseModelChangedEvent();
            }
        }

        public PointViewModel EndPoint
        {
            get 
            {
                return m_endPoint;
            }
            set
            {
                m_endPoint = value;
                OnPropertyChanged("EndPoint");
                RaiseModelChangedEvent();
            }
        }

        public ObservableCollection<string> Colors
        {
            get { return MainViewModel.Colors; }
        }

        public string Color
        {
            get 
            { 
                return G3DColors.GetColorName(m_line.Color.ToString()); 
            }
            set
            {
                m_line.Color = G3DColors.GetColor(value);
                OnPropertyChanged("Color");
                RaiseModelChangedEvent();
            }
        }

        #endregion

        private void GetPoints()
        {
            m_startPoint = null;
            m_endPoint = null;

            foreach (var p in AvailablePoints)
            {
                if (p.Point.Equals(m_line.StartPoint))
                {
                    m_startPoint = p;
                }
                else if (p.Point.Equals(m_line.EndPoint))
                {
                    m_endPoint = p;
                }

                if (m_startPoint != null && m_endPoint != null)
                {
                    break;
                }
            }
        }
    }
}
